package com.ruoyi.patrol.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.patrol.domain.PatrolTask;
import com.ruoyi.patrol.mapper.PatrolTaskMapper;
import com.ruoyi.patrol.service.IPatrolTaskService;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 巡查任务Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-05-08
 */
@Service
public class PatrolTaskServiceImpl implements IPatrolTaskService
{
    private static final Logger log = LoggerFactory.getLogger(PatrolTaskServiceImpl.class);

    @Autowired
    private PatrolTaskMapper patrolTaskMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询巡查任务
     * 
     * @param id 巡查任务主键
     * @return 巡查任务
     */
    @Override
    public PatrolTask selectPatrolTaskById(Long id)
    {
        return patrolTaskMapper.selectPatrolTaskById(id);
    }

    /**
     * 查询巡查任务列表
     * 
     * @param patrolTask 巡查任务
     * @return 巡查任务
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<PatrolTask> selectPatrolTaskList(PatrolTask patrolTask)
    {
        return patrolTaskMapper.selectPatrolTaskList(patrolTask);
    }

    /**
     * 新增巡查任务
     * 
     * @param patrolTask 巡查任务
     * @return 结果
     */
    @Override
    public int insertPatrolTask(PatrolTask patrolTask)
    {
        if(StringUtils.isEmpty(patrolTask.getUserName())){
            SysUser sysUser = userService.selectUserById(patrolTask.getUserId());
            patrolTask.setUserName(sysUser.getNickName());
        }
        patrolTask.setCreateTime(DateUtils.getNowDate());
        return patrolTaskMapper.insertPatrolTask(patrolTask);
    }

    /**
     * 修改巡查任务
     * 
     * @param patrolTask 巡查任务
     * @return 结果
     */
    @Override
    public int updatePatrolTask(PatrolTask patrolTask)
    {
        SysUser sysUser = userService.selectUserById(patrolTask.getUserId());
        patrolTask.setUserName(sysUser.getNickName());
        patrolTask.setUpdateTime(DateUtils.getNowDate());
        return patrolTaskMapper.updatePatrolTask(patrolTask);
    }

    /**
     * 批量删除巡查任务
     * 
     * @param ids 需要删除的巡查任务主键
     * @return 结果
     */
    @Override
    public int deletePatrolTaskByIds(Long[] ids)
    {
        return patrolTaskMapper.deletePatrolTaskByIds(ids);
    }

    /**
     * 删除巡查任务信息
     * 
     * @param id 巡查任务主键
     * @return 结果
     */
    @Override
    public int deletePatrolTaskById(Long id)
    {
        return patrolTaskMapper.deletePatrolTaskById(id);
    }

    /**
     * 开始巡查任务
     *
     * @param id 巡查任务主键
     * @return 结果
     */
    public int startPatrolTask(Long id){
        PatrolTask patrolTask = patrolTaskMapper.selectPatrolTaskById(id);
        if(patrolTask == null){
            throw new ServiceException("任务不存在");
        }

        if(!patrolTask.getProgress().equals("0")){
            return 1;
        }

        return patrolTaskMapper.copyPatrolTaskPoint(patrolTask);
    }

    /**
     * 结束巡查任务
     *
     * @param id 巡查任务主键
     * @return 结果
     */
    public int stopPatrolTask(Long id){
        PatrolTask patrolTask = patrolTaskMapper.selectPatrolTaskById(id);
        if(patrolTask == null){
            throw new ServiceException("任务不存在");
        }

        int p = patrolTaskMapper.countUnCheckPatrolTaskPoint(id);
        if(p > 0){
            throw new ServiceException("有 "+p+" 个点位还没有巡查");
        }
        patrolTask.setProgress("9");
        patrolTask.setUpdateBy(SecurityUtils.getUsername());
        patrolTask.setUpdateTime(new Date());
        return patrolTaskMapper.updatePatrolTask(patrolTask);
    }
}
